package me.zhengjie.modules.system.service.apply;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import me.zhengjie.exception.CommonUtilException;
import me.zhengjie.modules.system.domain.DisPartner;
import me.zhengjie.modules.system.domain.DisPartnerUser;
import me.zhengjie.modules.system.enums.ApplyWorkOrderTypeEnum;
import me.zhengjie.modules.system.service.DisPartnerService;
import me.zhengjie.modules.system.service.DisPartnerUserService;
import me.zhengjie.modules.system.service.WxUserService;
import me.zhengjie.modules.system.service.dto.DisChangeApplyDto;
import me.zhengjie.modules.system.service.dto.DisPartnerDto;
import me.zhengjie.modules.system.service.dto.DisPartnerQueryCriteria;
import me.zhengjie.modules.system.service.dto.DisPartnerWorkOrderDto;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DisPartnerChangeServiceImpl implements IApplyCallbackService{

    @Autowired
    DisPartnerService disPartnerService;

    @Autowired
    DisPartnerUserService disPartnerUserService;

    @Autowired
    WxUserService wxUserService;

    @Override
    public Integer getApplyType() {
        return ApplyWorkOrderTypeEnum.DisPartner.getType();
    }

    @Override
    public boolean execute(DisChangeApplyDto applyDto) {
        String json = applyDto.getApplyContent();
        if(StringUtils.isEmpty(json)){
           throw new CommonUtilException("申请内容不能为空");
        }
        DisPartnerWorkOrderDto disPartnerWorkOrderDto = JSONUtil.toBean(json, DisPartnerWorkOrderDto.class);
        if(StringUtils.isEmpty(disPartnerWorkOrderDto.getPartnerName())){
            throw new CommonUtilException("城市合伙人名称不能为空");
        }
        DisPartnerQueryCriteria criteria = new DisPartnerQueryCriteria();
        criteria.setPartnerName(disPartnerWorkOrderDto.getPartnerName());
        List<DisPartnerDto> oldList = disPartnerService.queryAll(criteria);
        if(!CollectionUtils.isEmpty(oldList) && oldList.size() > 1){
            throw new CommonUtilException("城市合伙人名称已存在");
        } else if(!CollectionUtils.isEmpty(oldList)){
            DisPartnerDto disPartnerDto = oldList.get(0);
            if(!disPartnerDto.getPartnerId().equals(disPartnerWorkOrderDto.getPartnerId())){
                throw new CommonUtilException("城市合伙人名称已存在");
            }
        }

        try {
            DisPartner disPartner = new DisPartner();
            BeanUtil.copyProperties(disPartnerWorkOrderDto,disPartner);
            if(disPartner.getPartnerId() == null){
                disPartner.setCreateTime(DateUtil.TimestampNow());
                disPartnerService.create(disPartner);
            } else {
                disPartner.setUpdateTime(DateUtil.TimestampNow());
                disPartnerService.update(disPartner);
            }
            disPartnerWorkOrderDto.getDisPartnerUserList().forEach(s -> s.setPartnerId(disPartner.getPartnerId()));
            disPartnerUserService.deleteByPartnerId(disPartner.getPartnerId());
            disPartnerWorkOrderDto.getDisPartnerUserList().forEach(s -> disPartnerUserService.create(s));

            List<String> openIdList = disPartnerWorkOrderDto.getDisPartnerUserList().stream().map(DisPartnerUser::getOpenId).collect(Collectors.toList());
            wxUserService.batchSetUserPartner(openIdList,disPartner);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
